home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / tut-code.lha / tut-code / circle / main.c < prev   
C/C++ Source or Header  |  1992-01-03  |  2KB  |  87 lines

  1. #include <InterViews/background.h>
  2. #include <InterViews/brush.h>
  3. #include <InterViews/canvas.h>
  4. #include <InterViews/color.h>
  5. #include <InterViews/place.h>
  6. #include <InterViews/session.h>
  7. #include <InterViews/style.h>
  8. #include <InterViews/window.h>
  9.  
  10. class Circle : public Glyph {
  11. public:
  12.     Circle(Coord radius, const Color*, const Brush*);
  13.     virtual ~Circle();
  14.  
  15.     virtual void request(Requisition&) const;
  16.     virtual void allocate(Canvas*, const Allocation&, Extension&);
  17.     virtual void draw(Canvas*, const Allocation&) const;
  18. private:
  19.     Coord radius_;
  20.     const Color* color_;
  21.     const Brush* brush_;
  22. };
  23.  
  24. Circle::Circle(Coord r, const Color* c, const Brush* b) {
  25.     radius_ = r;
  26.     Resource::ref(c);
  27.     color_ = c;
  28.     Resource::ref(b);
  29.     brush_ = b;
  30. }
  31.  
  32. Circle::~Circle() {
  33.     Resource::unref(color_);
  34.     Resource::unref(brush_);
  35. }
  36.  
  37. void Circle::request(Requisition& req) const {
  38.     Coord diameter = radius_ + radius_;
  39.     Requirement rx(diameter, 0, 0, 0.5);
  40.     Requirement ry(diameter, 0, 0, 0.5);
  41.     req.require(Dimension_X, rx);
  42.     req.require(Dimension_Y, ry);
  43. }
  44.  
  45. void Circle::allocate(Canvas* c, const Allocation& a, Extension& ext) {
  46.     ext.extend(c, a);
  47. }
  48.  
  49. void Circle::draw(Canvas* c, const Allocation& a) const {
  50.     const Coord r = radius_, x = a.x(), y = a.y();
  51.     const Coord p0 = 1.00000000 * r;
  52.     const Coord p1 = 0.89657547 * r;   // cos 30 * sqrt(1 + tan 15 * tan 15)
  53.     const Coord p2 = 0.70710678 * r;   // cos 45
  54.     const Coord p3 = 0.51763809 * r;   // cos 60 * sqrt(1 + tan 15 * tan 15)
  55.     const Coord p4 = 0.26794919 * r;   // tan 15
  56.     c->new_path();
  57.     c->move_to(x+p0, y);
  58.     c->curve_to(x+p2, y+p2, x+p0, y+p4, x+p1, y+p3);
  59.     c->curve_to(x, y+p0, x+p3, y+p1, x+p4, y+p0);
  60.     c->curve_to(x-p2, y+p2, x-p4, y+p0, x-p3, y+p1);
  61.     c->curve_to(x-p0, y, x-p1, y + p3, x-p0, y+p4);
  62.     c->curve_to(x-p2, y-p2, x-p0, y-p4, x-p1, y-p3);
  63.     c->curve_to(x, y-p0, x-p3, y-p1, x-p4, y-p0);
  64.     c->curve_to(x+p2, y-p2, x+p4, y-p0, x+p3, y-p1);
  65.     c->curve_to(x+p0, y, x+p1, y-p3, x+p0, y-p4);
  66.     c->close_path();
  67.     c->stroke(color_, brush_);
  68. }
  69.  
  70. int main(int argc, char** argv) {
  71.     Session* session = new Session("Himom", argc, argv);
  72.     Style* style = session->style();
  73.     session->run_window(
  74.     new ApplicationWindow(
  75.         new Background(
  76.         new VariableSpan(
  77.             new Margin(
  78.             new Circle(100, style->foreground(), new Brush(1.0)),
  79.             10.0
  80.             )
  81.         ),
  82.         style->background()
  83.         )
  84.     )
  85.     );
  86. }
  87.